package com.dfdchem.imd.kaiwu.manager;

import com.dfdchem.imd.kaiwu.util.Log;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 处理内外网链接的问题，切换为外网时需要吧内网地址转成外网。
 */
public class IntranetManager {
    private final static String TAG = "IntranetManager";

    public static final String URL_BASE_API = "http://172.21.10.61:81";
    public static final String URL_BASE_ADM = "http://172.21.10.61:91";
    public static final String URL_HELP = "http://172.21.10.54:88/#/help/index";

    /**
     * 内网地址转外网地址
     */
    private final static Map<String, String> Urls_Internet = new HashMap<String, String>(){{
        put("http://172.21.10.61:81/","http://218.28.49.66:29081/");
        put("http://172.21.10.61:91/","http://218.28.49.66:29091/");
        put("http://172.21.10.61:9000/","http://218.28.49.66:29000/");

        // 反向转换
    }};

    /**
     * 外网地址转内网地址
     */
    private final static Map<String, String> Urls_Intranet = new HashMap<String, String>(){{
        put("http://218.28.49.66:29081/", "http://172.21.10.61:81/");
        put("http://218.28.49.66:29091/", "http://172.21.10.61:91/");
        put("http://218.28.49.66:29000/", "http://172.21.10.61:9000/");
    }};

    /**
     * 内网网地址转换
     * @param oldUrl
     * @return
     */
    public static String convert(String oldUrl){
        String newUrl = null;

        Map<String, String> urls = DataManager.getInstance().isIntranet() ? Urls_Intranet : Urls_Internet;

        Set<String> keys = urls.keySet();
        for (String key: keys) {
            String value = urls.get(key);
            if (oldUrl.startsWith(key) && value != null) {
                newUrl = oldUrl.replace(key, value);
            }
        }

        Log.d(TAG, oldUrl + " => " + newUrl);
        return newUrl == null ? oldUrl : newUrl;
    }
}
